package zxing.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.virtualapp.R;
import io.virtualapp.abs.ui.VActivity;
import io.virtualapp.bean.SongBean;
import io.virtualapp.mylibrary.callback.BaseCallback;
import io.virtualapp.mylibrary.http.HttpUtils;
import io.virtualapp.mylibrary.util.SpUtils;
import okhttp3.Request;
import okhttp3.Response;
import zxing.activity.adapter.BackgAdapter;
import zxing.activity.bean.BackgBean;

public class LoveBackgroundActivity extends VActivity {
    List<BackgBean.DataBean> datas;
    @BindView(R.id.head_back)
    ImageView headBack;
    @BindView(R.id.head_titles)
    TextView headTitles;
    @BindView(R.id.head_center_title)
    TextView headCenterTitle;
    @BindView(R.id.head_right_title)
    TextView headRightTitle;
    @BindView(R.id.recy_backGround)
    RecyclerView recyBackGround;
    private BackgAdapter adapter;
    int selectPos;
    String imageFilePath, imageName;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_love_background);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        selectPos = intent.getIntExtra("pos", 0);
        getBack();
        getView();
    }

    public void getView() {
        headCenterTitle.setText("情书模板");
        headRightTitle.setVisibility(View.VISIBLE);
        headRightTitle.setText("保存");
        headBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        headRightTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                if (TextUtils.isEmpty(imageFilePath)) {
                    intent.putExtra("imageFilePath", datas.get(selectPos).getCodeX());
                } else {
                    intent.putExtra("imageFilePath", imageFilePath);
                }
                if (TextUtils.isEmpty(imageName)) {
                    intent.putExtra("imageName", datas.get(selectPos).getNameX());
                } else {
                    intent.putExtra("imageName", imageName);
                }
                intent.putExtra("selectPos", selectPos);
                setResult(0, intent);
                finish();
            }
        });
    }

    protected void initDatas() {
        adapter = new BackgAdapter(R.layout.item_call_backgroud, datas);
        GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
        recyBackGround.setLayoutManager(layoutManager);
        recyBackGround.setAdapter(adapter);
        adapter.setSelectPos(selectPos);
        initAction();
    }

    private void getBack() {
        HttpUtils.getInstance().postBackg(new BaseCallback<BackgBean>() {
            @Override
            public void onRequestBefore() {
            }

            @Override
            public void onFailure(Request request, Exception e) {
            }

            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
            @Override
            public void onSuccess(Response response, BackgBean o) {
                Log.e("TAG", o.toString());

                datas = o.getData();
                initDatas();
            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {

            }
        });
    }

    protected void initAction() {
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter madapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.relativ_p://整个item
                        adapter.setSelectPos(position);
                        imageFilePath = datas.get(position).getCodeX();
                        imageName = datas.get(position).getNameX();
                        selectPos = position;
                        break;
                    default:
                        break;
                }
            }
        });
    }

}
